package com.bayss.core.service.sku;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.skuTag.SkuTag;
import com.bayss.core.entity.skuTag.SkuTagExample;
import com.bayss.core.entity.skuTag.mapper.SkuTagMapper;
import com.bayss.core.util.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2019/12/9.
 */
@Service
public class SkuTagService {

    @Autowired
    private SkuTagMapper skuTagMapper;

    /**
     * 分页查询
     */
    public PageInfo<SkuTag> findByPage(Integer page, Integer pageSize) {
        SkuTagExample example = new SkuTagExample();
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page, pageSize);
        PageInfo<SkuTag> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> skuTagMapper.selectByExample(example));
        return result;
    }

    /**
     * 查询全部
     */
    public List<SkuTag> list() {
        SkuTagExample example = new SkuTagExample();
        example.setOrderByClause("create_time desc");
        List<SkuTag> list = skuTagMapper.selectByExample(example);
        return list;
    }

    /**
     * 根据id删除skuTag
     *
     * @param id
     */
    public void deleteById(Integer id) {
        skuTagMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据"id1,id2,id3..." 删除skuTag
     *
     * @param ids
     */
    public void deleteByIds(String ids) {
        for (String id : ids.split(",")) {
            skuTagMapper.deleteByPrimaryKey(Integer.valueOf(id));
        }
    }

    /**
     * 更新skuTag
     *
     * @param skuTag
     */
    public void update(SkuTag skuTag) {
        skuTag.setTagPic(FileUtil.formatImgUrl(skuTag.getTagPic()));
        skuTagMapper.updateByPrimaryKeySelective(skuTag);
    }


    /**
     * 新建skuTag
     *
     * @param skuTag
     * @return
     */
    public ResultMsg add(SkuTag skuTag) {
        skuTag.setCreateTime(new Date());
        skuTag.setTagPic(FileUtil.formatImgUrl(skuTag.getTagPic()));
        skuTagMapper.insertSelective(skuTag);
        return ResultMsg.success();
    }

}
